package app.poller.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import app.poller.R;
import app.poller.adapter.OptionArrayAdapter;
import app.poller.db.DBHelper;
import app.poller.entities.Contact;
import app.poller.entities.Option;
import app.poller.entities.Poll;

@SuppressLint("NewApi")
public class PollCreateActivity extends Activity implements OnClickListener {

	EditText pollQuestion;
	ListView pollOptionsListView;
	OptionArrayAdapter pollOptionsArrayAdapter;

	EditText newOptionEditText;
	Button newOptionAddButton;
	
	Button pollParticipantAddButton;
	
	ArrayList<Contact> participantContactList = new ArrayList<Contact>();
	ArrayList<Option> pollOptionList = new ArrayList<Option>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add);

		if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD)
			getActionBar().setDisplayHomeAsUpEnabled(true);

		// Get references to the poll question and option's list view
		pollQuestion = (EditText) findViewById(R.id.question);
		pollOptionsListView = (ListView) findViewById(R.id.option_listview);

		// Inflate footerView for listView where user can add a new option
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		RelativeLayout pollListViewFooter = (RelativeLayout) inflater.inflate(R.layout.option_add_view, null, false);
		newOptionEditText = (EditText) pollListViewFooter.findViewById(R.id.option_edittext);
		newOptionAddButton = (Button) pollListViewFooter.findViewById(R.id.option_add_button);
		newOptionAddButton.setOnClickListener(this);
		
		// Create adapter and attach it to ListView
		pollOptionsArrayAdapter = new OptionArrayAdapter(getApplicationContext(), R.layout.option_row, pollOptionList);
		pollOptionsListView.addFooterView(pollListViewFooter);
		pollOptionsListView.setAdapter(pollOptionsArrayAdapter);
		
		// Add listener to button that helps them add buttons
		pollParticipantAddButton = (Button) findViewById(R.id.add_poll_participants);
		pollParticipantAddButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_add, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		participantContactList = data.getExtras().getParcelableArrayList("result");
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < participantContactList.size(); i++) {
			
			if(i == participantContactList.size() - 1) {
				sb.append(participantContactList.get(i).getContactName());
				break;
			}
			sb.append(participantContactList.get(i).getContactName() + ", ");
		}
		pollParticipantAddButton.setText(sb.toString());
	}


	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.option_add_button:
			pollOptionList.add(new Option(newOptionEditText.getText().toString()));
			newOptionEditText.setText("");
			pollOptionsArrayAdapter.notifyDataSetChanged();
			pollOptionsListView.setSelectionFromTop(pollOptionsArrayAdapter.getCount() - 1, 20);
			break;
			
		case R.id.add_poll_participants:
			Intent x = new Intent(getApplicationContext(), AddContactActivity.class);
			x.putParcelableArrayListExtra("contactlist", participantContactList);
			startActivityForResult(x, 123);
		}
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;

		case R.id.create_poll:
			createPoll();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	private void createPoll() {
		for(int i = 0; i < pollOptionList.size(); i++) 
			pollOptionList.get(i).setOptionShortcut("" + i);
		
		
		Poll newPoll = new Poll(pollQuestion.getText().toString(), 
				pollOptionList, 
				participantContactList, 
				System.currentTimeMillis());
		
		DBHelper dbHandler = new DBHelper(getApplicationContext());
		dbHandler.addPoll(newPoll);
	}
}
